﻿using System;
using System.Data;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using HiauPdm.Bll;
using HiauPdm.Web;

namespace HiauPdm.Web
{
    public partial class CpSjBj : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        protected void Pager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            int iCpSjDl = -1;
            string sCpSjWhere = string.Empty;
            string sDjr = string.Empty;
            if (Request.QueryString["CpSjWhere"] == "[整件]")
            {
                iCpSjDl = 1;
            }
            else if (Request.QueryString["CpSjWhere"] == "[零部件]")
            {
                iCpSjDl = 0;
            }
            else if (Request.QueryString["CpSjWhere"] != null)
            {
                sCpSjWhere = Request.QueryString["CpSjWhere"].Trim();
            }
            if (sCpSjWhere == string.Empty)
            {
                sDjr = (string)Session["RealName"];
            }
            DataSet ds = null;
            if (iCpSjDl != -1 || sCpSjWhere != string.Empty)
            {
                if (!Page.IsPostBack)
                {
                    DataSet dsCount = BCpSjBj.GetCpSjBjCountByDjr(iCpSjDl, sCpSjWhere, sDjr);
                    if (dsCount != null)
                    {
                        Pager1.RecordCount = (int)dsCount.Tables[0].Rows[0][0];
                    }
                    if (Request.QueryString["PageIndex"] != null && BGlobal.IsNumeric(Request.QueryString["PageIndex"]))
                    {
                        Pager1.CurrentPageIndex = Convert.ToInt32(Request.QueryString["PageIndex"]);
                    }
                }
                ds = BCpSjBj.GetCpSjBjByDjr(iCpSjDl, sCpSjWhere, sDjr, Pager1.PageSize, Pager1.CurrentPageIndex);
            }
            if ((ds == null) || (ds.Tables[0].Rows.Count <= 0))
            {
                PanelList.InnerHtml = "<table cellspacing='0' cellpadding='0' border='0' style='border-style:none;border-collapse:collapse;width:832px;text-align:center;'><tr><td>[未找到产品设计记录]</td></tr></table>";
            }
            else
            {
                GridCpSj.DataSource = ds;
                GridCpSj.DataBind();
                SetCzQx();
                GridCpSj.SelectedIndex = -1;
                string sCpSjId = Request.QueryString["CpSjId"];
                if ((sCpSjId != null) && (sCpSjId.Trim() != string.Empty))
                {
                    for (int iIndex = 0; iIndex < GridCpSj.DataKeys.Count; iIndex++)
                    {
                        if (GridCpSj.DataKeys[iIndex].Value.ToString() == sCpSjId.Trim())
                        {
                            GridCpSj.SelectedIndex = iIndex;
                            break;
                        }
                    }
                }
            }
        }

        private void SetCzQx()
        {
            for (int i = 0; i < GridCpSj.DataKeys.Count; i++)
            {
                if (((int)GridCpSj.DataKeys[i].Values[1]) == 0)
                {
                    GridCpSj.Rows[i].Cells[6].Controls[3].Visible = false;
                    GridCpSj.Rows[i].Cells[6].Controls[5].Visible = false;
                }
                else if (((int)GridCpSj.DataKeys[i].Values[1]) == 1)
                {
                    GridCpSj.Rows[i].Cells[6].Controls[5].Visible = false;
                }
            }
            if (!IsAccess("编辑他人的产品设计"))
            {
                for (int i = 0; i < GridCpSj.DataKeys.Count; i++)
                {
                    if (GridCpSj.DataKeys[i].Values[2].ToString() != (string)Session["RealName"])
                    {
                        GridCpSj.Rows[i].Cells[7].Controls[1].Visible = false;
                    }
                }
            }
        }
    }
}